<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 内容区放表单 -->
    <el-form label-width="100px" ref="form" :model="ruleFrom" :rules="rules">
      <!-- 表单中的每一行，叫做 el-form-item -->
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="ruleFrom.old_pwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="ruleFrom.new_pwd"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="ruleFrom.re_pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="update">确认修改</el-button>
        <el-button>重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updateUserAPI, updateUserinfoAPI } from '@/api/user'
export default {
  data() {
    const newpwd = (rule, value, callback) => {
      if (value === this.ruleFrom.old_pwd) {
        callback(new Error('新密码和原密码不能相同'))
      } else {
        callback()
      }
    }
    const rePwd = (rule, value, callback) => {
      // 确认密码使用了这个规则，所以 value 表示输入框的确认密码
      if (value === this.ruleFrom.new_pwd) {
        callback()
      } else {
        callback(new Error('两次新密码不一致'))
      }
    }
    return {
      userInfo: { ...this.$store.state.user.user },
      ruleFrom: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      rules: {
        old_pwd: [
          { required: true, message: '原密码必填', trigger: 'blur' },
          { min: 6, max: 15, message: '长度必须是6~15位', trigger: 'blur' }
        ],
        new_pwd: [
          { required: true, message: '新密码必填', trigger: 'blur' },
          { min: 6, max: 15, message: '长度必须是6~15位', trigger: 'blur' },
          { validator: newpwd, trigger: 'blur' }
        ],
        re_pwd: [
          { required: true, message: '确认密码必填', trigger: 'blur' },
          { min: 6, max: 15, message: '长度必须是6~15位', trigger: 'blur' },
          { validator: rePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    updateUser() {
      // 完整的验证，验证通过，则调用API方法
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // console.log('验证通过，提交数据')
        const { data: res } = await updateUserAPI(this.userInfo)
        // console.log(res)
        if (res.code === 0) {
          this.$message.success(res.message)
          // 更新成功，更新vuex中的 user 数据
          this.$store.dispatch('user/getUser')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    resetForm() {
      // resetFields 恢复表单为最初的样子；去掉红色的表单验证提示
      this.$refs.form.resetFields()
      // 将vuex中的数据，更新到组件中
      this.userInfo = { ...this.$store.state.user.user }
    },
    update() {
      this.$refs.form.validate(async vaild => {
        if (!vaild) return
        // console.log(vaild)
        const { data: res } = await updateUserinfoAPI(this.ruleFrom)
        console.log(res)
        // 重置表单
        this.$refs.form.resetFields()
        // 重置密码，退出重新登陆
        this.$store.commit('user/updateToken', '')
        this.$store.commit('user/uodataUser', {})
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  font-size: 13px;
}

.el-form {
  width: 500px;
}
</style>
